#include <fstream>
#include "SystemManager.h"
#include "Employee.h"
#include "Manager.h"
#include "Boss.h"
#include "Worker.h"

SystemManager::SystemManager() {
    init();
}

void SystemManager::printMenu(){

    cout << "1.增加职工信息" << endl;
    cout << "2.显示职工信息" << endl;
    cout << "3.删除职工" << endl;
    cout << "4.修改职工" << endl;
    cout << "5.查找职工" << endl;
    cout << "6.按编号排序" << endl;
    cout << "7.清空所有文档" << endl;
    cout << "8.退出管理程序" << endl;
}

void SystemManager::exitSystem(){
    cout << "退出程序" << endl;
    exit(0);
}

void SystemManager::addWorker(){
    int addNum;
    cout << "请输入添加的人员数量" << endl;
    cin >> addNum;

    if (addNum <= 0)
    {
        cout << "输入错误" << endl;
        return;
    }

    int newNum = addNum + num;

    Worker ** newWorkerArray = new Worker *[newNum];

    for (int i = 0; i < addNum; i++)
    {
        cout << "开始添加第" << i+1 << "人" << endl;
        cout << "请输入人员id" << endl;
        int id;
        cin >> id;

        int index = findById(id);
        if (index != NULL) {
            cout << "该id已存在" << endl;
            i--;
            continue;
        }
        cout << "请输入人员姓名" << endl;
        string name;
        cin >> name;

        cout << "请输入人员类型" << endl;
        cout << "1.普通员工" << endl;
        cout << "2.经理" << endl;
        cout << "3.老板" << endl;

        int type = 0;
        cin >> type;

        if (type == 1) {
            Employee *employee = new Employee(id, name);
            newWorkerArray[num+i] = employee;
        }else if (type == 2) {
            Manager *manager = new Manager(id,name);
            newWorkerArray[num+i] = manager;
        }else if (type == 3) {
            Boss *boss = new Boss(id,name);
            newWorkerArray[num+i] = boss;
        }

    }

    //旧数组拷贝到新数组
    if (num > 0){
        for (size_t i = 0; i < num; i++)
        {
            newWorkerArray[i] = workerArray[i];
        }
    }

    //释放旧数组
    if (workerArray != NULL)
    {
        delete[] workerArray;
        workerArray = NULL;
    }
    num = num + addNum;
    workerArray = newWorkerArray;

    save();
}

void SystemManager::showWorkerInfo() {
    if (num == 0) {
        cout << "---暂无员工---" << endl;
        return;
    }
    for (int i = 0; i < num; ++i) {
        Worker* worker =  workerArray[i];
        worker->printInfo();
        cout << "------" << endl;
    }
}

void SystemManager::deleteWorker() {
    int id = 0;
    cout << "请输入将要删除的员工id" << endl;
    cin >> id;

    int index = findById(id);
    if (index == NULL) {
        cout << "该id不存在" << endl;
        return;
    }

    delete workerArray[index];
    for (int i = index;i < num-1; ++i) {
        workerArray[index] = workerArray[index+1];
    }
    num--;
    save();
}

void SystemManager::updateWorker() {
    int id = 0;
    cout << "请输入id" << endl;
    cin >> id;

    int index = findById(id);
    if (index == NULL) {
        cout << "该id不存在" << endl;
        return;
    }

    cout << "请输入人员姓名" << endl;
    string name;
    cin >> name;

    cout << "请输入人员类型" << endl;
    cout << "1.普通员工" << endl;
    cout << "2.经理" << endl;
    cout << "3.老板" << endl;

    int type = 0;
    cin >> type;

    //删除之前的对象
    Worker* worker = workerArray[index];
    delete worker;
    if (type == 1) {
        Employee *employee = new Employee(id, name);
        workerArray[index] = employee;
    }else if (type == 2) {
        Manager *manager = new Manager(id,name);
        workerArray[index] = manager;
    }else if (type == 3) {
        Boss *boss = new Boss(id,name);
        workerArray[index] = boss;
    }
    save();
}

void SystemManager::findWorker(){
    int id = 0;
    cout << "请输入id" << endl;
    cin >> id;
    int index = findById(id);
    Worker* worker = workerArray[index];
    if (worker != NULL) {
      worker->printInfo();  
    }else {
        cout << "未找到该id的员工" << endl;
    }
}

void SystemManager::sortWorker(){
    if (workerArray == NULL) {
        return;
    }
    for (int i = 0;i < num; ++i) {
        for (int j = i+1;j < num;j++) {
            if (workerArray[i]->id > workerArray[j]->id) {
                Worker *workerTemp =  workerArray[i];
                workerArray[i] = workerArray[j];
                workerArray[j] = workerTemp;
            }
        }
    }
    save();
}

void SystemManager::deleteAll(){
    delete[] workerArray;
    workerArray = NULL;
    num = 0;
    save();
}

int SystemManager::findById(int id) {
    for (int i = 0; i < num; ++i) {
        Worker* worker =  workerArray[i];
        int workerId = worker->id;
        if (workerId == id) {
            return i;
        }
    }
    return NULL;
}

void SystemManager::save() {
    ofstream ofs;
    ofs.open("worker.txt", ios::out);

    if (num != 0) {
        for (int i=0;i<num;i++) {
            Worker * worker = workerArray[i];
            ofs << worker->id << " " << worker->name << " " << worker->type <<endl;
        }
    }

    ofs.close();
}

void SystemManager::init() {
    ifstream ifs;
    ifs.open("worker.txt", ios::in);

    int worker_num_from_file = getWorkerNumFromFile();
    num = worker_num_from_file;
    if (worker_num_from_file == 0) {
        return;
    }

    workerArray = new Worker *[num];
    int id;
    string name;
    int type;

    int i = 0;
    while (ifs >> id && ifs >>name && ifs >>type) {
        Worker* worker = NULL;
        if (type == 1) {
            worker = new Employee(id,name);
        }else if (type == 2) {
            worker = new Manager(id,name);
        }else {
            worker = new Boss(id,name);
        }

        workerArray[i] = worker;
        i++;
    }

    ifs.close();
}

int SystemManager::getWorkerNumFromFile() {
    ifstream ifs;
    ifs.open("worker.txt", ios::in);

    if (!ifs.is_open()) {
        return 0;
    }

    int id;
    string name;
    int type;

    int n = 0;
    while (ifs >> id && ifs >>name && ifs >>type) {
        n++;
    }
    ifs.close();
    return n;
}